package vic.controller;

import vic.logic.GameModuleLogic;
import vic.sendreceive.param.PlayBoardParamDefinition;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;

public class Playboard {
	public User hoster;
	public Room room;
	public GameModuleLogic gameLogic;
	public boolean isPlaying;
	public Playboard(Room room, User hoster, GameModuleLogic logic) {
		this.isPlaying = false;
		this.room = room;
		this.hoster = hoster;
		this.gameLogic = logic;
		this.gameLogic.roomId = room.getId();
		this.joinPlayboard(hoster);
	}
	public void joinPlayboard(User user){
		try {
			Room lastRoom = user.getLastJoinedRoom();
			if(lastRoom != null){
				user.removeJoinedRoom(lastRoom);
			}
			this.room.addUser(user);
		} catch (SFSJoinRoomException e) {
			e.printStackTrace();
		}
	}
	public void leavePlayboard(User user){
		this.room.removeUser(user);
	}
	public ISFSObject packWhenCreateRoom(){
        ISFSObject resObj = SFSObject.newInstance();	 
        resObj.putInt(PlayBoardParamDefinition.hostId, this.hoster.getId());
        resObj.putUtfString(PlayBoardParamDefinition.playboardName,this.room.getName());
        resObj.putInt(PlayBoardParamDefinition.roomId,this.room.getId());
        return resObj;
	}
	public ISFSObject packWhenJoinRoom(){
		ISFSObject resObj = SFSObject.newInstance();	 
		String userList = "";
		for (User user : room.getUserList()) {
			userList += user.getId() + "_" + user.getName() + ";";
		}
		resObj.putUtfString(PlayBoardParamDefinition.userList, userList);
        return resObj;
	}
}
